package com.crr.mq;

import com.crr.bean.Order;
import com.crr.bean.OrderDetail;
import com.crr.common.utils.RedisUtils;
import com.crr.service.OrderService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Component
@Slf4j
public class OrderConsumer {
    @Autowired
    RedisUtils redisUtils;
    @Autowired
    OrderService orderService;

    /**
     * 只执行一次 初始化方法
     * 队列中没有消息则阻塞 有消息则执行
     */
    @PostConstruct
    public void init() {
        log.info("消费者正在等待消息");
    }

    /**
     * 监听订单队列
     * @param message
     * @param channel
     */
    @RabbitListener(queues = "orderQueue")
    public void handlerOrder(Message message, Channel channel) {
        //获得消息
        String msg = new String(message.getBody());
        String[] split = msg.split("@");
        //获得消息中的用户id和商品id
        int userId = Integer.parseInt(split[0]);
        int goodsId = Integer.parseInt(split[1]);
        String orderNum = split[2];

        //生成订单
        String key = "goods:id:" + goodsId;
        //从缓存中获取商品信息
        String price = redisUtils.get(key + ":price");
        String goodsname = redisUtils.get(key + ":goodsname");
        //订单信息
        Order order = new Order();
        order.setOrderNum(orderNum);
        order.setMoney(Double.parseDouble(price.toString()));
        order.setUserId(userId);
        //订单明细
        OrderDetail orderDetail = new OrderDetail();
        List<OrderDetail> list=new ArrayList<>();
        orderDetail.setNum(1);
        orderDetail.setGoodsname(goodsname.toString());
        orderDetail.setPrice(Double.parseDouble(price.toString()));
        orderDetail.setGoodsId(goodsId);
        list.add(orderDetail);
        order.setOrderDetails(list);

        //生成订单
        int res = orderService.saveOrder(order);
        if(res==1){
            try {
                channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }else {
            try {
                channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,true);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

    }
    @RabbitListener(queues = "lazy-queue")
    public void handlerLayOrder(Message message, Channel channel){
        String id = new String(message.getBody());
        //查询订单状态 判断
        int res = orderService.updateStatus(Integer.parseInt(id));
        if(res==1){
            log.info("订单id=["+id+"]的订单未支付，已经取消");
            //取消之后库存回退

        }
        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

}
